<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信号" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入微信号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!--<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">-->
      <!--<el-button-->
      <!--type="primary"-->
      <!--icon="el-icon-plus"-->
      <!--size="mini"-->
      <!--@click="handleAdd"-->
      <!--v-hasPermi="['system:mobileUser:add']"-->
      <!--&gt;新增</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--<el-button-->
      <!--type="success"-->
      <!--icon="el-icon-edit"-->
      <!--size="mini"-->
      <!--:disabled="single"-->
      <!--@click="handleUpdate"-->
      <!--v-hasPermi="['system:mobileUser:edit']"-->
      <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--<el-button-->
      <!--type="danger"-->
      <!--icon="el-icon-delete"-->
      <!--size="mini"-->
      <!--:disabled="multiple"-->
      <!--@click="handleDelete"-->
      <!--v-hasPermi="['system:mobileUser:remove']"-->
      <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--<el-button-->
      <!--type="warning"-->
      <!--icon="el-icon-download"-->
      <!--size="mini"-->
      <!--@click="handleExport"-->
      <!--v-hasPermi="['system:mobileUser:export']"-->
      <!--&gt;导出</el-button>-->
      <!--</el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <img :src="scope.row.avatar"  width="50px" height="50px" >
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUserName(scope.row)">
            {{scope.row.userName}}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="微信号" align="center" prop="微信号" />
      <el-table-column label="手机号码" align="center" prop="phone" />

      <el-table-column label="性别" align="center" prop="sex":formatter="statusFormat1" />
      <!--      -->
      <el-table-column label="余额" align="center" prop="money" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleMoney(scope.row)">
            {{scope.row.money}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="冻结金额" align="center" prop="amount" />

      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--&lt;!&ndash;<template slot-scope="scope">&ndash;&gt;-->
      <!--&lt;!&ndash;<el-button&ndash;&gt;-->
      <!--&lt;!&ndash;size="mini"&ndash;&gt;-->
      <!--&lt;!&ndash;type="text"&ndash;&gt;-->
      <!--&lt;!&ndash;icon="el-icon-edit"&ndash;&gt;-->
      <!--&lt;!&ndash;@click="handleUpdate(scope.row)"&ndash;&gt;-->
      <!--&lt;!&ndash;v-hasPermi="['system:mobileUser:edit']"&ndash;&gt;-->
      <!--&lt;!&ndash;&gt;查看</el-button>&ndash;&gt;-->
      <!--&lt;!&ndash;&lt;!&ndash;<el-button&ndash;&gt;&ndash;&gt;-->
      <!--&lt;!&ndash;&lt;!&ndash;size="mini"&ndash;&gt;&ndash;&gt;-->
      <!--&lt;!&ndash;&lt;!&ndash;type="text"&ndash;&gt;&ndash;&gt;-->
      <!--&lt;!&ndash;&lt;!&ndash;icon="el-icon-delete"&ndash;&gt;&ndash;&gt;-->
      <!--&lt;!&ndash;&lt;!&ndash;@click="handleDelete(scope.row)"&ndash;&gt;&ndash;&gt;-->
      <!--&lt;!&ndash;&lt;!&ndash;v-hasPermi="['system:mobileUser:remove']"&ndash;&gt;&ndash;&gt;-->
      <!--&lt;!&ndash;&lt;!&ndash;&gt;删除</el-button>&ndash;&gt;&ndash;&gt;-->
      <!--&lt;!&ndash;</template>&ndash;&gt;-->
      <!--</el-table-column>-->

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open2" width="550px">
      <el-table :data="moneyList" style="width: 100%">
        <el-table-column prop="money" label="余额" width="150"></el-table-column>
        <el-table-column prop="moneyType" label="变更类型" width="150" :formatter="statusFormat2">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="200">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total1>0"
        :total="total1"
        :page.sync="moneyParams.pageNum"
        :limit.sync="moneyParams.pageSize"
        @pagination="handleMoney"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确定</el-button>
      </div>

    </el-dialog>
    <!-- 添加或修改用户端app对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="冻结金额" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入冻结金额" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="微信open_id" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入微信open_id" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="用户头像" prop="avatar">
          <img :src="form.avatar" alt="11"  >

        </el-form-item>
        <!--<el-form-item label="用户头像" prop="avatar">-->
        <!--<el-input v-model="form.avatar" placeholder="请输入用户头像" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="用户头像" prop="password">-->
        <!--<el-input v-model="form.password" placeholder="请输入用户头像" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="关联后台用户userId,如果存在则说明是被推广商家邀请入驻" prop="userId">-->
        <!--<el-input v-model="form.userId" placeholder="请输入关联后台用户userId,如果存在则说明是被推广商家邀请入驻" />-->
        <!--</el-form-item>-->
        <el-form-item label="可提现金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入可提现金额" />
        </el-form-item>
        <el-form-item label="冻结金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入冻结金额" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">状态</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="冻结金额" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入冻结金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
        <!--<el-button @click="cancel">取 消</el-button>  -->
      </div>
    </el-dialog>

    <!--    用户信息-->
    <el-dialog title="用户详情" :visible.sync="showUserInfo" width="500px">
      <el-form ref="form" :model="userInfoList" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="userInfoList.userName"   disabled/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone" >
          <el-input v-model="userInfoList.phone"   disabled />
        </el-form-item>
        <el-form-item label="状态" prop="statusName">
          <el-input v-model="userInfoList.statusName"   disabled/>
        </el-form-item>
        <el-table
          :data="userInfoList.sysAddresses"
          border
          style="width: 100%">
          <el-table-column
            prop="name"
            label="姓名">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="手机号">
          </el-table-column>
          <el-table-column
            prop="allAddress"
            label="地址">
          </el-table-column>
        </el-table>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import { listUser, moneyList, getUser, delUser, addUser, updateUser, exportUser } from "@/api/system/mobileUser";

  export default {
    name: "User",
    data() {
      return {
        userInfoList:{},
        showUserInfo:false,
        // 遮罩层
        loading: true,
        statusOptions:[],
        sexOptions:[],
        moneyList:[],
        total1:0,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 用户端app表格数据
        userList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open2:false,
        moneyParams:{
          mobileUserId:undefined,
          pageNum: 1,
          pageSize: 10,
        },
        // 查询参数
        queryParams: {
          mobileUserId:undefined,
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          phone: undefined,
          openId: undefined,
          sex: undefined,
          address: undefined,
          avatar: undefined,
          password: undefined,
          userId: undefined,
          money: undefined,
          amount: undefined,
          status: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();

      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("sys_user_sex").then(response => {
        this.sexOptions = response.data;
      });
      this.getDicts("sys_money_type").then(response => {
        this.Options = response.data;
      });
    },
    methods: {
      handleUserName(row){
        this.userInfoList = {}
        console.log(row)
        this.showUserInfo = true
        getUser(row.mobileUserId).then(res=>{
          var obj = res.data
          this.statusOptions.forEach((items,i)=>{
            if(items.dictValue ==obj.status ) {
              obj.statusName = items.dictLabel
            }
          })
          if(!!obj.sysAddresses && obj.sysAddresses.length>0){
            obj.sysAddresses.forEach((items,i)=>{
              items.allAddress = items.area + items.address
            })

          }
          console.log(obj)
          this.userInfoList=obj
        })
      },
      statusFormat2(row, column) {
        return this.selectDictLabel(this.Options, row.moneyType);
      },
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      statusFormat1(row, column) {
        return this.selectDictLabel(this.sexOptions, row.sex);
      },
      /** 查询用户端app列表 */
      getList() {
        this.loading = true;
        listUser(this.queryParams).then(response => {
          response.rows.forEach((items,i)=>{
            items.avatar =  process.env.VUE_APP_BASE_API + items.avatar
          })
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.open2 = false;
        this.reset();
      },

      // 表单重置
      reset() {
        this.form = {
          mobileUserId: undefined,
          userName: undefined,
          phone: undefined,
          openId: undefined,
          sex: undefined,
          address: undefined,
          avatar: undefined,
          password: undefined,
          userId: undefined,
          money: undefined,
          amount: undefined,
          status: "0",
          delFlag: undefined
        };
        this.resetForm("form");
      },
      /**查看余额*/
      handleMoney(row){
        this.moneyParams.mobileUserId = row.mobileUserId
        console.log(this.moneyParams)
        moneyList(this.moneyParams).then(response => {
        // moneyList({mobileUserId:this.moneyParams.mobileUserId}).then(response => {
          console.log(response)
          this.moneyList = response.rows;
          this.open2 = true;
          this.total1 = response.total;
          this.title = "查看余额变动";
        })
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.mobileUserId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用户端app";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const mobileUserId = row.mobileUserId || this.ids
        getUser(mobileUserId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看app端用户";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.mobileUserId != undefined) {
              updateUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const mobileUserIds = row.mobileUserId || this.ids;
        this.$confirm('是否确认删除用户端app编号为"' + mobileUserIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(mobileUserIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有用户端app数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
